import { toCanvas } from 'qrcode';
import type { QRCodeRenderersOptions, QRCodeSegment } from 'qrcode';
type ContentType = string | QRCodeSegment[];
export type ConfigOptions = QRCodeRenderersOptions & {};
export interface OptionsParams {
    canvas: any,
    content: ContentType,
    width?: number,
    options?: QRCodeRenderersOptions;
}
const renderQrcode = ({
    canvas,
    content,
    width = 0,
    options: params = {}
}: OptionsParams) => {
    const options = JSON.parse(JSON.stringify(params));
    options.errorCorrectionLevel = 'H'
    options.scale = width / 10; // 缩放比例控制着二维码的模糊度
    return toCanvas(canvas, content, options)
}
// 转化为二维码
export const toQrCode = function (data: QRCodeRenderersOptions): Promise<String> {
    return renderQrcode(data).then((res: any) => {
        return res
    }) as Promise<String>
}
// 下载
export const downloadCode = (url: string) => {
    const linkelement = document.createElement('a')
    linkelement.setAttribute('href', url)
    linkelement.setAttribute('download', 'code.jpg')
    linkelement.click()
}
